package com.qfedu.stussm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.stussm.dao.ScoreDao;
import com.qfedu.stussm.dto.ScoreDto;
import com.qfedu.stussm.entity.Score;
import com.qfedu.stussm.service.intf.ScoreService;
import com.qfedu.stussm.vo.PageBeanVo;
import com.qfedu.stussm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    private ScoreDao scoreDao;
    @Override
    public int add(Score score) {
        return scoreDao.add(score);
    }

    @Override
    public PageBeanVo selectAll(String stu_name,int page, int limit) {
        System.out.println(stu_name);
        if(stu_name != null && stu_name.length() > 0){
            stu_name = "%"+stu_name+"%";
        } else{
            stu_name = null;
        }
        PageHelper.startPage(page, limit);
        PageInfo<ScoreDto> scorePageInfo = new PageInfo<>(scoreDao.selectAll(stu_name));
        return new PageBeanVo(scorePageInfo.getTotal(), scorePageInfo.getList());
    }

    @Override
    public R update(Score score) {
        if(scoreDao.update(score) > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R del(int id) {
        if (scoreDao.del(id) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
}
